<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.name" placeholder="广告位"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getPositions">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="positions" highlight-current-row v-loading="loading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="id" label="推广位ID" width="100" sortable>
			</el-table-column>
			<el-table-column prop="mark" label="名称" width="200" sortable>
			</el-table-column>
			<el-table-column prop="pid" label="PID" width="200" sortable>
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemovePosition" :disabled="this.sels.length===0">批量删除</el-button>
			<!-- <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="20" :total="total" style="float:right;">
			</el-pagination> -->
		</el-col>

		<!--编辑界面-->
		<el-dialog title="编辑" v-model="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="淘宝推广位ID" prop="pid">
					<el-input v-model="editForm.pid" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="备注" prop="mark">
					<el-input v-model="editForm.mark" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editPosition" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--新增界面-->
		<el-dialog title="新增广告位" v-model="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="淘宝推广位ID" prop="pid">
					<el-input v-model="addForm.pid" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="备注" prop="mark">
					<el-input v-model="addForm.mark" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addPosition" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>
<script>
	import { getPositions, addPosition, editPosition, delPosition, batchDelPosition } from '../../api/api';
	export default {
		data() {
			return {
				filters: {
				},
				loading: false,
				positions: [],
				sels: [],//列表选中列
				total: 0,
				addFormVisible: false,
				addLoading: false,
				editFormVisible: false,
				editLoading: false,
				//新增界面数据
				addForm: {
					mark: '',
					pid: ''
				},
				addFormRules: {
					pid: [
						{ required: true, message: '请输入淘宝推广位ID', trigger: 'blur' }
					],
					mark: [
						{ required: true, message: '请输入备注', trigger: 'blur' }
					]
				},
				editForm: {
					mark: '',
					pid: ''
				},
				editFormRules: {
					pid: [
						{ required: true, message: '请输入淘宝推广位ID', trigger: 'blur' }
					],
					mark: [
						{ required: true, message: '请输入备注', trigger: 'blur' }
					]
				},
			}
		},
		methods: {
			getPositions: function() {
				this.loading = true;
				getPositions().then((res) => {
					let { info, data } = res
					if (!info.ok) {
						this.$message({
						  message: info.message,
						  type: 'error',
						  duration: 1500
						});
						return
					}

					this.positions = data;
					this.loading = false;
				});
			},
			handleCurrentChange(val) {
				this.page = val;
				this.getPositions();
			},
			//显示新增界面
			handleAdd: function () {
				this.addFormVisible = true;
				this.addForm = {
					mark: '',
					pid: '',
				};
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.loading = true;
					//NProgress.start();
					let para = { id: row.id };
					delPosition(para).then((res) => {
						let { info, data } = res
						this.loading = false;
						if (!info.ok) {
							this.$message({
								message: info.message,
								type: 'error',
								duration: 1500
							});
							return
						}
						this.getPositions();
					});
				}).catch(() => {

				});
			},
			//新增
			addPosition: function () {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.addForm);
							addPosition(para).then((res) => {
								let { info, data } = res
								this.addLoading = false;
								if (!info.ok) {
									this.$message({
										message: info.message,
										type: 'error',
										duration: 1500
									});
									return
								}
								this.$refs['addForm'].resetFields();
								this.addFormVisible = false;
								this.getPositions();
							});
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			editPosition: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.editForm);
							editPosition(para).then((res) => {
								let { info, data } = res
								this.editLoading = false;
								if (!info.ok) {
									this.$message({
										message: info.message,
										type: 'error',
										duration: 1500
									});
									return
								}
								this.$refs['editForm'].resetFields();
								this.editFormVisible = false;
								this.getPositions();
							});
						});
					}
				});
			},
			//批量删除
			batchRemovePosition: function () {
				let ids = this.sels.map(item => item.id).toString();
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.loading = true;
					//NProgress.start();
					let para = { ids: ids };
					batchDelPosition(para).then((res) => {
						let { info, data } = res
						this.loading = false;
						if (!info.ok) {
							this.$message({
								message: info.message,
								type: 'error',
								duration: 1500
							});
							return
						}
						this.getPositions();
					});
				}).catch(() => {

				});
			}
		},
		mounted() {
			this.getPositions();
		}
	};

</script>

<style scoped>

</style>